package com.pkk.cloud.support.admin.controller.interior.resources;

import static com.pkk.cloud.support.admin.constand.OauthResourceConstand.DEFAULT_RESOURCE_IS_DELETED_FALSE;
import static com.pkk.cloud.support.admin.constand.OauthResourceConstand.DEFAULT_RESOURCE_IS_SHOW_TRUE;
import static com.pkk.cloud.support.admin.constand.OauthResourceConstand.DEFAULT_RESOURCE_PID_ZONE;

import com.pkk.cloud.support.admin.constand.OauthResourceConstand.ResourceLevel;
import com.pkk.cloud.support.admin.constand.OauthResourceConstand.ResourceType;
import com.pkk.cloud.support.admin.dto.req.resources.OauthResourceButtonAddReqDto;
import com.pkk.cloud.support.admin.dto.req.resources.OauthResourceCateoryAddReqDto;
import com.pkk.cloud.support.admin.dto.req.resources.OauthResourceIdReqDto;
import com.pkk.cloud.support.admin.dto.req.resources.OauthResourceMenuAddReqDto;
import com.pkk.cloud.support.admin.dto.req.resources.OauthResourcePidReqDto;
import com.pkk.cloud.support.admin.dto.req.resources.OauthResourceRoleBindReqData;
import com.pkk.cloud.support.admin.dto.req.resources.OauthResourceUpdateIdReqDto;
import com.pkk.cloud.support.admin.dto.req.resources.OauthServerClientAllResourceSimpleReqDto;
import com.pkk.cloud.support.admin.dto.resp.resources.OauthResourceSimpleRespDto;
import com.pkk.cloud.support.admin.dto.resp.server.OauthServerClientSimpleRespDto;
import com.pkk.cloud.support.admin.entity.resource.PenguinSysResource;
import com.pkk.cloud.support.admin.service.PenguinSysResourceService;
import com.pkk.cloud.support.oauth2.client.aspect.annotation.PreAuth;
import com.pkk.cloud.support.oauth2.common.utils.SecurityUtils;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 资源菜单等管理器
 * @author: peikunkun
 * @create: 2020-04-02 11:11
 **/
@Slf4j
@RestController
@RequestMapping("/oauth/resources")
public class OauthResourcesController {

  @Resource
  private PenguinSysResourceService penguinSysResourceService;

  /**
   * @Description: 资源与角色信息绑定
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:57
   */
  @PreAuth
  @PostMapping("bindResourceRole")
  public CommonResponse<Boolean> bindResourceRole(
      @Validated @RequestBody CommonRequest<OauthResourceRoleBindReqData> request) {
    return ResponseUtil.success(penguinSysResourceService.bindResourceRole(request.getBody()));
  }


  /**
   * @Description: 获取所有菜单-包括服务/资源/按钮
   * @Author: peikunkun
   * @Date: 2020/4/9 下午7:12
   */
  @PreAuth
  @PostMapping("getServerClientAllResource")
  public CommonResponse<List<OauthServerClientSimpleRespDto>> getServerClientAllResource(
      @Validated @RequestBody CommonRequest<OauthServerClientAllResourceSimpleReqDto> request) {
    return ResponseUtil.success(penguinSysResourceService.getServerClientAllResource(request.getBody()));
  }


  /**
   * @Description: 获取资源信息依据父id
   * @Param: request
   * @Author: peikunkun
   * @Date: 2020/4/11 0011 下午 7:41
   */
  @PreAuth
  @PostMapping("getResourceByPid")
  public CommonResponse<List<OauthResourceSimpleRespDto>> getResourceByPid(
      @Validated @RequestBody CommonRequest<OauthResourcePidReqDto> request) {
    return ResponseUtil.success(penguinSysResourceService.getResourceByPid(request.getBody()));
  }


  /**
   * @Description: 添加目录分类信息
   * @Param: request
   * @Author: peikunkun
   * @Date: 2020/4/18 0018 下午 3:11
   */
  @PreAuth
  @PostMapping("bindCateory")
  public CommonResponse<Boolean> bindCateory(
      @Validated @RequestBody CommonRequest<OauthResourceCateoryAddReqDto> request) {
    final String userId = SecurityUtils.getUserId();
    final PenguinSysResource penguinSysResource = new PenguinSysResource();
    BeanUtils.copyProperties(request.getBody(), penguinSysResource);
    penguinSysResource.setCreateName(userId);
    penguinSysResource.setCreateTime(new Date());
    penguinSysResource.setResourceLevel(ResourceLevel.CATEORY.getLevel());
    penguinSysResource.setResourceType(ResourceType.CATEORY.getType());
    penguinSysResource.setIsShow(DEFAULT_RESOURCE_IS_SHOW_TRUE);
    penguinSysResource.setIsDeleted(DEFAULT_RESOURCE_IS_DELETED_FALSE);
    penguinSysResource.setResourcePid(DEFAULT_RESOURCE_PID_ZONE);

    final ArrayList<PenguinSysResource> penguinSysResources = new ArrayList<>();
    penguinSysResources.add(penguinSysResource);
    return ResponseUtil.success(penguinSysResourceService.addResource(penguinSysResources, userId));
  }


  /**
   * @Description: 添加菜单
   * @Param: request
   * @Author: peikunkun
   * @Date: 2020/4/18 0018 下午 3:11
   */
  @PreAuth
  @PostMapping("bindMenu")
  public CommonResponse<Boolean> bindMenu(
      @Validated @RequestBody CommonRequest<OauthResourceMenuAddReqDto> request) {
    final String userId = SecurityUtils.getUserId();
    final PenguinSysResource penguinSysResource = new PenguinSysResource();
    BeanUtils.copyProperties(request.getBody(), penguinSysResource);
    penguinSysResource.setCreateName(userId);
    penguinSysResource.setCreateTime(new Date());
    penguinSysResource.setResourceLevel(ResourceLevel.MENU.getLevel());
    penguinSysResource.setResourceType(ResourceType.MENU.getType());
    penguinSysResource.setIsShow(DEFAULT_RESOURCE_IS_SHOW_TRUE);
    penguinSysResource.setIsDeleted(DEFAULT_RESOURCE_IS_DELETED_FALSE);

    final ArrayList<PenguinSysResource> penguinSysResources = new ArrayList<>();
    penguinSysResources.add(penguinSysResource);
    return ResponseUtil.success(penguinSysResourceService.addResource(penguinSysResources, userId));
  }


  /**
   * @Description: 添加按钮
   * @Param: request
   * @Author: peikunkun
   * @Date: 2020/4/18 0018 下午 3:11
   */
  @PreAuth
  @PostMapping("bindButton")
  public CommonResponse<Boolean> bindButton(
      @Validated @RequestBody CommonRequest<List<OauthResourceButtonAddReqDto>> request) {
    final String userId = SecurityUtils.getUserId();

    final ArrayList<PenguinSysResource> penguinSysResources = new ArrayList<>();
    request.getBody().stream().forEach(a -> {
      final PenguinSysResource penguinSysResource = new PenguinSysResource();
      BeanUtils.copyProperties(a, penguinSysResource);
      penguinSysResource.setCreateName(userId);
      penguinSysResource.setCreateTime(new Date());
      penguinSysResource.setResourceLevel(ResourceLevel.BUTTON.getLevel());
      penguinSysResource.setResourceType(ResourceType.BUTTON.getType());
      penguinSysResource.setIsShow(DEFAULT_RESOURCE_IS_SHOW_TRUE);
      penguinSysResource.setIsDeleted(DEFAULT_RESOURCE_IS_DELETED_FALSE);
      penguinSysResources.add(penguinSysResource);
    });
    return ResponseUtil.success(penguinSysResourceService.addResource(penguinSysResources, userId));
  }


  /**
   * @Description: 获取资源信息依据id
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<com.pkk.cloud.support.admin.dto.resp.resources.OauthResourceSimpleRespDto>
   * @Author: peikunkun
   * @Date: 2020/4/18 0018 下午 3:44
   */
  @PreAuth
  @PostMapping("getResourceById")
  public CommonResponse<OauthResourceSimpleRespDto> getResourceById(
      @Validated @RequestBody CommonRequest<OauthResourceIdReqDto> request) {
    return ResponseUtil.success(penguinSysResourceService.getResourceById(request.getBody().getResourceId()));
  }


  /**
   * @Description: 删除资源信息
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/21 0021 下午 3:31
   */
  @PreAuth
  @PostMapping("delResourceById")
  public CommonResponse<Boolean> delResourceById(
      @Validated @RequestBody CommonRequest<OauthResourceIdReqDto> request) {
    final String userId = SecurityUtils.getUserId();
    return ResponseUtil.success(penguinSysResourceService.delResourceById(request.getBody(), userId));
  }


  /**
   * @Description: 更新资源依据id
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/21 0021 下午 4:05
   */
  @PreAuth
  @PostMapping("updateResourceById")
  public CommonResponse<Boolean> updateResourceById(
      @Validated @RequestBody CommonRequest<OauthResourceUpdateIdReqDto> request) {
    final String userId = SecurityUtils.getUserId();
    return ResponseUtil.success(penguinSysResourceService.updateResourceById(request.getBody(), userId));
  }


}
